<?php
class RM_Admin_MyReservations_CancelController extends RM_Controller
{
    function loadJsonAction()
    {
        try {
            $reservation = $this->_getReservation();
        } catch (RM_Exception $e) {
            return array(
                'data' => array(
                    'success' => false,
                    'status' => 0,
                    'message' => $e->getMessage()
                )
            );
        }

        if ($reservation->cancel !== RM_MyReservation_Status::PENDING) {
            return array(
                'data' => array(
                    'success' => false,
                    'status' => 2,
                    'message' => $this->_translate->_(
                        'Admin.MyReservations.Reservation.Cancel.Errors', 
                        'ReservationIsNotInPendingStatus'
                    )
                )
            );
        }

        $config = new RM_Config();
        $currencySymbol = $config->getCurrencySymbol();

        $billingModel = new RM_Billing();

        $reservationJson = new stdClass();
        $reservationJson->id = $reservation->id;
        $reservationJson->total_paid = $currencySymbol.$billingModel->getPaymentsTotal($reservation);
        $reservationJson->cancel_request = $reservation->cancel_request;
        $reservationJson->cancel = $reservation->cancel;

        return array(
            'data' => array(
                'success' => true,
                'status' => 1,
                'reservation' => $reservationJson
            )
        );
    }

    function cancelJsonAction()
    {
        try {
            $reservation = $this->_getReservation();
        } catch (RM_Exception $e) {
            return array(
                'data' => array(
                    'success' => false,
                    'message' => $e->getMessage()
                )
            );
        }

        $module = new RM_Module_MyReservations();
        try {
            $module->approveCancel($reservation);
        } catch (RM_Exception $e) {
            return array(
                'data' => array(
                    'success' => false,
                    'message' => $e->getMessage()
                )
            );
        }

        return array(
            'data' => array('success' => true)
        );
    }

    /**
     * @throws RM_Exception
     * @return RM_Reservation_Row
     */
    protected function _getReservation()
    {
        $reservationID = $this->_getParam('reservation_id');
        if ($reservationID == null) {
            throw new RM_Exception($this->_translate->_(
                'Admin.MyReservations.Reservation.Cancel.Errors',
                'ReservationIDNotSpecified'
            ));
        }
        $reservationModel = new RM_Reservations();
        $reservation = $reservationModel->find($reservationID)->current();
        if ($reservation == null) {
            throw new RM_Exception($this->_translate->_(
                'Admin.MyReservations.Reservation.Cancel.Errors',
                'WrongReservationID'
            ));
        }
        return $reservation;
    }
}